<?php

/**
 * Furnizor form base class.
 *
 * @package    vvberp
 * @subpackage form
 * @author     Flo
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 16976 2009-04-04 12:47:44Z fabien $
 */
class BaseFurnizorForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                             => new sfWidgetFormInputHidden(),
      'name'                           => new sfWidgetFormInput(),
      'email'                          => new sfWidgetFormInput(),
      'contact'                        => new sfWidgetFormInput(),
      'updated_at'                     => new sfWidgetFormDateTime(),
      'created_at'                     => new sfWidgetFormDateTime(),
      'created_by'                     => new sfWidgetFormPropelChoice(array('model' => 'sfGuardUser', 'add_empty' => true)),
      'updated_by'                     => new sfWidgetFormPropelChoice(array('model' => 'sfGuardUser', 'add_empty' => true)),
      'cerere_furnizor_list'           => new sfWidgetFormPropelChoiceMany(array('model' => 'Cerere')),
      'furnizor_has_tip_furnizor_list' => new sfWidgetFormPropelChoiceMany(array('model' => 'TipFurnizor')),
    ));

    $this->setValidators(array(
      'id'                             => new sfValidatorPropelChoice(array('model' => 'Furnizor', 'column' => 'id', 'required' => false)),
      'name'                           => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'email'                          => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'contact'                        => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'updated_at'                     => new sfValidatorDateTime(array('required' => false)),
      'created_at'                     => new sfValidatorDateTime(array('required' => false)),
      'created_by'                     => new sfValidatorPropelChoice(array('model' => 'sfGuardUser', 'column' => 'id', 'required' => false)),
      'updated_by'                     => new sfValidatorPropelChoice(array('model' => 'sfGuardUser', 'column' => 'id', 'required' => false)),
      'cerere_furnizor_list'           => new sfValidatorPropelChoiceMany(array('model' => 'Cerere', 'required' => false)),
      'furnizor_has_tip_furnizor_list' => new sfValidatorPropelChoiceMany(array('model' => 'TipFurnizor', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('furnizor[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Furnizor';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['cerere_furnizor_list']))
    {
      $values = array();
      foreach ($this->object->getCerereFurnizors() as $obj)
      {
        $values[] = $obj->getCerereId();
      }

      $this->setDefault('cerere_furnizor_list', $values);
    }

    if (isset($this->widgetSchema['furnizor_has_tip_furnizor_list']))
    {
      $values = array();
      foreach ($this->object->getFurnizorHasTipFurnizors() as $obj)
      {
        $values[] = $obj->getTipFurnizorId();
      }

      $this->setDefault('furnizor_has_tip_furnizor_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveCerereFurnizorList($con);
    $this->saveFurnizorHasTipFurnizorList($con);
  }

  public function saveCerereFurnizorList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['cerere_furnizor_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(CerereFurnizorPeer::FURNIZOR_ID, $this->object->getPrimaryKey());
    CerereFurnizorPeer::doDelete($c, $con);

    $values = $this->getValue('cerere_furnizor_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new CerereFurnizor();
        $obj->setFurnizorId($this->object->getPrimaryKey());
        $obj->setCerereId($value);
        $obj->save();
      }
    }
  }

  public function saveFurnizorHasTipFurnizorList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['furnizor_has_tip_furnizor_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(FurnizorHasTipFurnizorPeer::FURNIZOR_ID, $this->object->getPrimaryKey());
    FurnizorHasTipFurnizorPeer::doDelete($c, $con);

    $values = $this->getValue('furnizor_has_tip_furnizor_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new FurnizorHasTipFurnizor();
        $obj->setFurnizorId($this->object->getPrimaryKey());
        $obj->setTipFurnizorId($value);
        $obj->save();
      }
    }
  }

}
